package com.hdj.shuzu;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: HeDongJia
 * @Description:
 * @Date: Create in 23:45 2021/7/23
 * @Modified By:
 */
/*
1.两个相同的数字，若进行异或，则结果是0
2.0异或任何数字，结果是任何数字
3.异或还满足交换律和结合律
 */
public class SingleNumber01 {
    //只出现一次的数字Java实现
    public int doubleNumber(int[] nums){
        long startTime = System.currentTimeMillis();
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i=0;i<nums.length;i++){
            if(map.containsKey(nums[i])){
                map.put(nums[i],0);
            }else{
                map.put(nums[i],1);
            }
        }
        for(Map.Entry entry:map.entrySet()){
            if((int)entry.getValue()==0){
                System.out.println("程序运行时间"+(System.currentTimeMillis()-startTime)+"毫秒");
                return (int)entry.getKey();
            }
        }
        System.out.println("程序运行的时间:"+(System.currentTimeMillis()-startTime)+"毫秒");
        return 0;
    }
    public int singleNumber(int[] nums) {
        long a= System.currentTimeMillis();//获取当前系统时间(毫秒)
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<nums.length;i++){
            if(map.containsKey(nums[i])){
                map.put(nums[i],0);
            }else{
                map.put(nums[i],1);
            }
        }
        for(Map.Entry entry : map.entrySet()){
            if((int)entry.getValue() == 1){
                System.out.println(System.currentTimeMillis()-a+"毫秒");
                return (int)entry.getKey();
            }
        }
        System.out.println("程序运行的时间:"+(System.currentTimeMillis()-a)+"毫秒");
        return 0;
    }
    //异或运算符
    public int singleNumber02(int[] nums){
        for (int i = 1; i < nums.length; i++) {
            nums[0]=nums[0]^nums[i];
        }
        return nums[0];
    }

    public static void main(String[] args) {
        SingleNumber01 singleNumber01 = new SingleNumber01();
        int[] nums={9,1,4,5,6,2,9};
        int[] nums02={3,1,2,1,3};
        System.out.println(singleNumber01.singleNumber02(nums02));
        //System.out.println(singleNumber01.doubleNumber(nums));
    }
}
